package org.hackystat.devcathlon.ui.page.scoreboard.scorespanel;

import java.util.Iterator;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.Model;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.login.LoginResponsePage;
import org.hackystat.devcathlon.ui.page.scoreboard.gameinfopanel.GameInfoPage;
import org.hackystat.devcathlon.ui.page.teams.profile.TeamsProfile;
import org.hackystat.utilities.tstamp.Tstamp;

/**
 * A display for ongoing matches.
 * 
 * @author John Zhou, Robin Raqueno, Daniel Arakaki, Scheller Sanchez
 */
public class ScoresPanel extends Panel {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Constructs the match panel for match manager page.
   * 
   * @param wicketId Component id.
   * @param matchList A list of matches.
   */
  public ScoresPanel(String wicketId, List<String> matchList) {
    super(wicketId);
    
    String noMatch = "";
    if (matchList == null) {
      noMatch = "There are no matches to list...";     
    }
    
    DataView dataview = new DataView("ItemList", new ListDataProvider(matchList)) {

      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(Item item) {
        final String matchName = (String) item.getModelObject();
        item.add(new Label("MatchLabel", matchName));
        item.add(new Link("MatchLink") {
          
          private static final long serialVersionUID = 1L;

          // Upon clicking this link, go to Match page.
          @Override
          public void onClick() {
            try {
              if (DevcathlonSession.get().getEmail() == null) {
                setResponsePage(new LoginResponsePage(MatchManager.getInstance()
                    .getMatch(matchName)));
              }
              else {
                setResponsePage(new GameInfoPage(MatchManager.getInstance()
                    .getMatch(matchName)));                
              }
            }
            catch (Exception e) {
              // if there is an exception error
              e.printStackTrace();
            }
          }
        });
     // Make start time
        XMLGregorianCalendar startTime = Tstamp.makeTimestamp();
        String startStringTime = " ";
        String endStringTime = "";
        String status = "";
        try {
          if (Tstamp.lessThan(startTime, 
              MatchManager.getInstance().getMatch(matchName).getEndTime())) {
            
            startStringTime = " " + startTime.getMonth() + "/" + startTime.getDay() + "/" 
            + startTime.getYear();
            endStringTime = " " 
              + MatchManager.getInstance().getMatch(matchName).getEndTime().getMonth() + "/"
              + MatchManager.getInstance().getMatch(matchName).getEndTime().getDay() + "/"
              + MatchManager.getInstance().getMatch(matchName).getEndTime().getYear();
            status = "On going";
          }
          else {
            endStringTime = " " + startTime.getMonth() + "/" + startTime.getDay() + "/" 
            + startTime.getYear();
            startStringTime = " "
            + MatchManager.getInstance().getMatch(matchName).getEndTime().getMonth() + "/"
            + MatchManager.getInstance().getMatch(matchName).getEndTime().getDay() + "/"
            + MatchManager.getInstance().getMatch(matchName).getEndTime().getYear();
            status = "Completed";
          }
        }
        catch (Exception e) {
          // if there is an exception error
          e.printStackTrace();
        }
        item.add(new Label("StatusLabel", status));
        item.add(new Label("StartTimeLabel", startStringTime));
        item.add(new Label("EndTimeLabel", endStringTime));
        WebMarkupContainer row1 = new WebMarkupContainer("Team1Row");
        WebMarkupContainer row2 = new WebMarkupContainer("Team2Row");

        Iterator<Team> iTeam = MatchManager.getInstance().getMatch(matchName).getTeams().iterator();
        final Team team1 = iTeam.next();
        row1.add(new Link("Team1Link") {
          private static final long serialVersionUID = 1L;

          /** Upon clicking this link, go to Team page. */
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team1.getName());
            setResponsePage(new TeamsProfile());
          }
        }
        .add(new Label("Team1Label", team1.getName())));
        Integer team1Points = MatchManager.getInstance().getMatch(matchName).getTeamPoints(team1);
        row1.add(new Label("Team1PointsLabel", team1Points.toString()));
        
        final Team team2 = iTeam.next();
        row2.add(new Link("Team2Link") {
          private static final long serialVersionUID = 1L;

          /** Upon clicking this link, go to Team page. */
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team2.getName());
            setResponsePage(new TeamsProfile());
          }
        }
        .add(new Label("Team2Label", team2.getName())));
        Integer team2Points = MatchManager.getInstance().getMatch(matchName).getTeamPoints(team2);
        row2.add(new Label("Team2PointsLabel", team2Points.toString()));
        
        if (status.compareTo("Completed") == 0) {
          if (team1Points > team2Points) {
            row1.add(new AttributeModifier("bgcolor", new Model("#6B90DB")));
          }
          else {
            row2.add(new AttributeModifier("bgcolor", new Model("#6B90DB")));
          }
        }

        item.add(row1);
        item.add(row2);
      }
    };
    add(dataview);
    add(new Label("noMatches", noMatch));
  }
}
